﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using SapB1WebPOS.BLL;
using System.Data;
using Newtonsoft.Json;
using SapB1WebPOS.Utils;

namespace SapB1WebPOS
{
    public partial class Permission : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["IsSAPUser"] != null)
            {
                if (!(bool)HttpContext.Current.Session["IsSAPUser"])
                {
                    Response.Redirect("AccessDenied.htm");
                }
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllOnlineUser()
        {
            try
            {
                string companyDB = string.Empty;

                if (HttpContext.Current.Session["CompanyDB"] != null)
                {
                    companyDB = HttpContext.Current.Session["CompanyDB"].ToString();
                }
                if (companyDB.Length > 0)
                {
                    PermissionBLL obj = new PermissionBLL();
                    DataSet dsData = obj.GetAllOnlineUser(companyDB);
                    if (dsData != null)
                    {
                        return JsonConvert.SerializeObject(dsData.Tables[0]);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string UpdatePermission(string oUser)
        {
            try
            {

                Model.OnlineUser user = JsonConvert.DeserializeObject<Model.OnlineUser>(oUser);

                if (user != null)
                {
                    string companyDB = string.Empty;

                    if (HttpContext.Current.Session["CompanyDB"] != null)
                    {
                        companyDB = HttpContext.Current.Session["CompanyDB"].ToString();
                    }
                    if (companyDB.Length > 0)
                    {
                        PermissionBLL pms = new PermissionBLL();
                        string count = pms.UpdateOnlineUserPermission(companyDB, user.Code, user.U_PurchaseOrder, user.U_PurchaseRequest,
                            user.U_SalesQuotation, user.U_SalesOrder, user.U_GRPO, user.U_WebPOS);
                        if (Utils.Utils.IsNumber(count))
                        {
                            if (int.Parse(count) > 0)
                            {
                                return "SUCCESS";
                            }
                        }
                        else
                        {
                            return count;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return ex.Message;
            }
            return string.Empty;
        }
    }
}